﻿using System;
using System.Security.Principal;

using Dxr.Police.Models;

namespace Dxr.Police.Web
{
    public class Principal : IPrincipal
    {
        private Identity _identity;
        private string[] _roles;

        public Principal(Identity identity, params string[] roles)
        {
            _identity = identity;
            _roles = roles;
        }

        public IIdentity Identity => _identity;

        public bool IsInRole(string role)
        {
            if (string.IsNullOrWhiteSpace(role))
                throw new ArgumentNullException("role");

            if (_roles == null || _roles.Length < 1)
                return false;

            foreach (var item in _roles)
            {
                if (string.Equals(item, role, StringComparison.OrdinalIgnoreCase))
                    return true;
            }

            return false;
        }
    }

    public class Identity : IIdentity
    {
        private string _credentialId;
        private UserInfo _userInfo;
        private string _authenticationType;

        public Identity(string credentialId, UserInfo userInfo, string authenticationType)
        {
            if (string.IsNullOrEmpty(credentialId))
                throw new ArgumentNullException("credentialId");

            _credentialId = credentialId;
            _userInfo = userInfo;
            _authenticationType = authenticationType;
        }

        public UserInfo UserInfo => _userInfo;

        public string Name => _credentialId;

        public string AuthenticationType => _authenticationType;

        public bool IsAuthenticated => UserInfo != null;
    }
}
